package com.mistyrain.service.impl;

import com.mistyrain.entity.*;
import com.mistyrain.feign.OrderFeign;
import com.mistyrain.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderFeign orderFeign;

    @Override
    public String save(long mid, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Order order = new Order();
        Menu menu = new Menu();
        menu.setId(mid);
        order.setUser(user);
        order.setMenu(menu);
        order.setDate(new Date());
        orderFeign.save(order);
        return "redirect:/account/redirect/order";
    }

    @Override
    public OrderVO findAllByUid(int page, int limit, HttpSession session) {
        User user = (User) session.getAttribute("user");
        return orderFeign.findAllByUid(user.getId(), page, limit);
    }

    @Override
    public OrderVO findAllByState(int page, int limit) {
        return orderFeign.findAllByState(0, page, limit);
    }

    @Override
    public String updateState(long id, int state, HttpSession session) {
        Admin admin = (Admin) session.getAttribute("admin");
        orderFeign.updateState(id,state,admin.getId());
        return "redirect:/account/redirect/order_handler";
    }
}
